package com.example.servlet;

import com.alibaba.fastjson2.JSON;
import com.example.service.PetService;
import com.example.service.impl.PetServiceImpl;
import com.example.util.AjaxResult;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/petNameRepeatServlet")
public class PetNameRepeatServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String petName = req.getParameter("petName");
        Boolean isRepeat = false;
        if(StringUtils.isNotBlank(petName)){
            PetService petService = new PetServiceImpl();
            isRepeat = petService.checkPetNameRepeat(petName);
        }
        resp.setCharacterEncoding("UTF-8");
        PrintWriter pw = resp.getWriter();
        pw.print(JSON.toJSONString(AjaxResult.success(isRepeat)));
        pw.flush();
        pw.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
